/**
 * 推荐码管理页面管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：推荐码管理
     创建日期：2015-10-12
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	showCmList();
	 $(".date").datepicker({
	        todayBtn: "linked",
	        keyboardNavigation: false,
	        forceParse: false,
	        calendarWeeks: true,
	        autoclose: true
	    });
});

/**
 * 查询推荐码
 * @returns
 */
var showCmList = function(){

	var remark = $('#q_remark').val();
	var code = $('#q_code').val();
	var status = $('#q_status').val();
	var theSendTime=$("#theSendTime").val();
	var theUseTime=$("#theUseTime").val();
	var datas =  {"p":"codeManage","m":"list","remark":remark,"code":code,"status":status,"theSendTime":theSendTime,"theUseTime":theUseTime};
	var columnDefine = {"id":"主键","status":"状态","remark":"备注","code":"邀请码","useDate":"使用日期","createDate":"创建日期"};
	
	var formatters={
	            "createDate": function (column, row)
	            {
    				return row.createDate;
	            },
	            "useDate": function (column, row)
	            {
	            	return row.useDate;
	            },
	            "status": function (column, rowData)
	            {
    				return rowData.status == 0 ?'未使用':'已使用';
	            }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showNewRemarkWin();">添加备注</button>';
		buttons+='</p>';
	   hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};

var getTheSureTime=function(emp){
	return emp>9?emp:"0"+emp;
}
//时间格式转换
var timeFormatTrans=function(time){
	var newTime= new Date(time);
	var rowText=newTime.getFullYear()+'-'+getTheSureTime((newTime.getMonth()+1))+'-'
	+getTheSureTime(newTime.getDate())+' '+getTheSureTime(newTime.getHours())+':'
	+getTheSureTime(newTime.getMinutes())+':'+getTheSureTime(newTime.getSeconds());
	return rowText;
};

/**
 * 显示新建窗口
 * @returns
 */
var showNewWin = function(){
	$('#newCodeWin input').val('');
	$('#newCodeWin').modal('show');
};

/**
 * 创建推荐码
 * @returns
 */
var newCode = function(){
	
	  var num = $('#newCodeWin .num').val();
	  var remark = $('#newCodeWin .remark').val();
	  var datas = {"p":"codeManage","m":"new","remark":remark,"num":num};
	  var json = common.ajaxRequest('service', datas ,'POST');
	  if( json && json.result == 0 )
	  {
		  $('#q_remark').val('');
		  $('#q_code').val('');
		  $('#q_status').val('-1');
		  showCmList();
		  $('#newCodeWin').modal('hide');
		  common.showMsg('info', '', '创建成功!');
	  }
	  else
	  {
		  common.showMsg('error', '', json.message);
	  }	  
};

/**
 * 显示新建窗口
 * @returns
 */
var showNewRemarkWin = function(){
	
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#pop_win').modal('hide');
	$('#newRemarkWin input').val('');
	$('#newRemarkWin .codeId').val(json.id);
	$('#newRemarkWin').modal('show');
};

/**
 * 添加备注
 * @returns
 */
var addRemark = function(){
	
	  var id = $('#newRemarkWin .codeId').val();
	  var remark = $('#newRemarkWin .remark').val();
	  var datas = {"p":"codeManage","m":"modifyRemark","remark":remark,"id":id};
	  var json = common.ajaxRequest('service', datas ,'POST');
	  if( json && json.result == 0 )
	  {
		  showCmList();
		  $('#newRemarkWin').modal('hide');
		  common.showMsg('info', '', '创建成功!');
	  }
	  else
	  {
		  common.showMsg('error', '', json.message);
	  }	  
	
};